#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <time.h>
#include <string.h>
#include "Parameters.h"
#include "simulation.h"
#include "editor.h"

void simulate(SDL_Surface *ecran)
{
    SDL_Surface *carreVert = SDL_LoadBMP("carreVert.bmp");
    SDL_Surface *carreRouge = SDL_LoadBMP("carreRouge.bmp");
    SDL_Surface *robot = SDL_LoadBMP("robotRouge.bmp");

    SDL_SetColorKey(robot, SDL_SRCCOLORKEY, SDL_MapRGB(robot->format, 255, 255, 255));

    int (*map)[SIZE_HEIGHT][SIZE_WIDTH];
    map=newMap();
    SDL_Rect positionFond;

    int i;
    int j;

    for(j=0; j<SIZE_HEIGHT; j++)
    {
        for(i=0; i<SIZE_WIDTH; i++)

        {


            positionFond.x = 100*i;
            positionFond.y = 100*j;
            if((*map)[j][i]==0)
            {

                SDL_BlitSurface(carreVert, NULL, ecran, &positionFond);
            }
            else
            {


                SDL_BlitSurface(carreRouge, NULL, ecran, &positionFond);
            }

        }

    }


    SDL_Rect  positionRobot;


    positionRobot.x = 400;
    positionRobot.y = 400;

    // a supprimer
    SDL_Event event;
    int continuer = 1;
    deplace(&positionRobot,map,0,0);

    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
        case SDL_QUIT:
            continuer = 0;
            break;
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {
            case SDLK_UP: // Flèche haut
                SDL_BlitSurface(carreVert, NULL, ecran, &positionRobot);
                positionRobot.y-=100;

                break;
            case SDLK_DOWN: // Flèche bas
                SDL_BlitSurface(carreVert, NULL, ecran, &positionRobot);

                positionRobot.y+=100;
                break;
            case SDLK_RIGHT: // Flèche droite
                SDL_BlitSurface(carreVert, NULL, ecran, &positionRobot);

                positionRobot.x+=100;
                break;
            case SDLK_LEFT: // Flèche gauche
                SDL_BlitSurface(carreVert, NULL, ecran, &positionRobot);

                positionRobot.x-=100;
                break;
            }
            break;
        }





        SDL_BlitSurface(robot, NULL, ecran, &positionRobot);


        /*SDL_Flip(ecran);*/


        SDL_Flip(ecran);


    }
    SDL_FreeSurface(robot);
    SDL_FreeSurface(carreVert);
    SDL_FreeSurface(carreRouge);
    free(map);

}
int deplace(SDL_Rect *position,int (*m)[SIZE_HEIGHT][SIZE_WIDTH], int dx, int dy)
{
    if((*m)[(position->y+dy)/100][(position->x+dx)/100]==0 && position->y+dy<100*SIZE_HEIGHT && position->x+dx<100*SIZE_WIDTH)
    {
        position->y+=dy;
        position->x+=dx;
        return 1;

    }
    return 0;
}
// struct vehicule
typedef struct
{
    SDL_Rect position;
    int  (*p)[16][8];
    int (*sense_wall)[8];
} vehicule;
// Vehicule constructor
vehicule randomVehicule()
{
    vehicule v;
    v.position.x=0;
    v.position.y=0;

    int tab[16][8];

    int i;
    int j;
    for(i =0; i<16; i++)
    {
        for(j=0; i<8; j++)
        {
            tab[i][j]= rand()%2;
        }
    }

    v.p=&tab;
    int(tab1)[8];
    v.sense_wall=&tab1;

    return v;
}
// new generation of rules
int (*mixRule(int (*p1)[16][8],int (*p2)[16][8]))[16][8]
{
    int tab[16][8];

    int i;
    int j;
    for(i =0; i<16; i++)
    {
        for(j=0; i<8; j++)
        {
            if(*p1[i][j]==*p2[i][j])
            {
                tab[i][j]=*p1[i][j];
            }
            else
            {
                tab[i][j]= rand()%2;
            }
        }

    }
    int  (*p)[16][8];
    p= &tab;
    return p;

}
//new generation of vehicule
vehicule mixVehicule(vehicule* v1,vehicule* v2)
{
    vehicule v;
    v.position.x =0;
    v.position.y=0;
    v.p= mixRule(v1->p,v2->p);

    return v;
}
void sense(vehicule* v,SDL_Rect *position,int (*map)[SIZE_HEIGHT][SIZE_WIDTH])
{
    int i;
    int j;
    int n=0;

    for(i=-1; i<1;i++)
    {
        for(i=-1; i<1;i++)
        {
            if(i==0 && j==0)
            {
                i++;
            }

            int a=i;
            int b=j;
            int m=0;
            while((*map)[(position->y)/100+a][(position->x)/100+b]!= 1)
            {
                m++;
                if( position->y/100+a == SIZE_HEIGHT || position->x/100+b == SIZE_WIDTH )
                {
                    (*(v->sense_wall))[n]=0;
                    n++;
                    a+=i;
                    b+=j;

                }
                (*(v->sense_wall))[n]=m;
                n++;

            }
        }
    }
}
